// @flow
import React, { Component } from 'react';
// import { Provider } from 'react-redux';
// import { ConnectedRouter } from 'react-router-redux';
// import Routes from '../routes';

import { BrowserRouter } from 'react-router-dom';
import ReactDOM from 'react-dom'
import Header from './Header';
import Main from './Main';
import Reload from './Reload';
import Footer from './Footer';
import Login from './Login';
import RightSlider from './RightSlider';
import '../static/css/common.css';
import '../static/css/login.css';
import Windows from './Windows';
import eventProxy from '../events/eventProxy'
import { inject, observer } from 'mobx-react';


@inject('coins')
@observer
export default class Root extends Component<Props> {

	constructor(props) {
		super(props)

		this.state = {
			permissionCode: this.props.coins.currentPermission,

		}
		this.showRightSlider = this.showRightSlider.bind(this)
		this.updateRootComponents = this.updateRootComponents.bind(this)
	}

	showRightSlider() {
		console.log('打开侧边栏')
		setTimeout(function () {
			eventProxy.trigger('showSlider', 'msg');
		}, 20)

	}

	updateRootComponents(permissionCode) {
		this.setState({
			permissionCode: permissionCode
		})
	}

	componentDidMount() {
		// 更改显示的标题
		let coinsMiner = this.props.coins.currentCoinsMiner
		document.title = coinsMiner
	}

	componentDidUpdate() {
		// let permissionCode = this.props.coins.currentPermission
		// console.log('执行了更新组件，并且权限码为：'+permissionCode)
  }

	render() {
		/* permission: 1(用户名或密码错误)  2(普通用户)   3(管理员)*/
		let permissionCode = this.props.coins.currentPermission
		console.log('触发渲染，当前权限码为：'+ permissionCode)
		let shouldRender

		console.log(permissionCode == 3)
		
		if ((permissionCode == 0) || (permissionCode ==1)) {
			console.log('-----渲染 登录页-----')
			shouldRender = (
				<div>
					<Login updateRootComponents={this.updateRootComponents} />
				</div>
			)
		} else if ((permissionCode == 2) || (permissionCode == 3)) {
			console.log('-----渲染 登录页-----')
			shouldRender = (
				<div>
					<Windows showRightSlider={this.showRightSlider} />
					<Header />
					<Main />
					<Footer />
					<RightSlider />
					<Reload />
				</div>
			)
		}

		return (
			<BrowserRouter>
				<div>
					{shouldRender}
				</div>
			</BrowserRouter>
		);
	}
}
